<template>
  <div id="app">
    <!-- 导航栏 -->
    <el-menu mode="horizontal"
             @select="handleSelect"
             :default-active="this.$route.path">
      <el-menu-item index="/home">
        首页
      </el-menu-item>

      <!-- 未登录时显示 -->
      <el-menu-item index="/login"
                    class="float-right"
                    v-if="!loginState">
        登录/注册
      </el-menu-item>

      <!-- 已登录时显示 -->
      <el-submenu index="/user"
                  v-if="loginState"
                  class="float-right">
        <template slot="title">欢迎你，{{userAccount}}</template>
        <el-menu-item index="/user">个人中心</el-menu-item>
        <el-menu-item index="/notice">通知</el-menu-item>
        <el-menu-item index="/usermanage"
                      v-if="userPower !== 0">用户管理</el-menu-item>
        <el-menu-item index="/logout">退出</el-menu-item>
      </el-submenu>
    </el-menu>
    <router-view></router-view>

    <!-- 退出登录对话框 -->
    <el-dialog title="提示"
               :visible.sync="dialogVisible"
               width="30%">
      确定退出登录吗？
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="danger"
                   @click="logout">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      dialogVisible: false
    }
  },
  methods: {
    handleSelect (key, keyPath) {
      if (key === '/logout') {
        this.dialogVisible = true
      } else {
        this.$router.push(key)
      }
    },
    logout () {
      this.dialogVisible = false
      this.$store.commit('logout') // 修改vuex中的登录数据
      if (this.$route.path !== '/home') { this.$router.push('/') } // 回到首页
    }
  },
  computed: {
    loginState () {
      return this.$store.getters.loginState
    },
    userAccount () {
      return this.$store.getters.userAccount
    },
    userPower () {
      return this.$store.getters.userPower
    }
  }
}
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

.float-right {
  float: right !important;
}

.router-link {
  text-decoration: none;
}
</style>
